home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-01-29 | 10.2 KB | 489 lines | [TEXT/MMCC] |
-
- //============================================================================
- //----------------------------------------------------------------------------
- // Prefs.c
- //----------------------------------------------------------------------------
- //============================================================================
-
-
- #include "Externs.h"
- #include <Folders.h>
- #include <GestaltEqu.h>
- #include <Script.h>
-
-
- #define kPrefCreatorType 'zade'
- #define kPrefFileType 'zadP'
- #define kPrefFileName "\pGlypha Prefs"
- #define kDefaultPrefFName "\pPreferences"
- #define kPrefsStringsID 160
- #define kPrefsFNameIndex 1
-
-
- Boolean CanUseFindFolder (void);
- Boolean GetPrefsFPath (long *, short *);
- Boolean CreatePrefsFolder (short *);
- Boolean GetPrefsFPath6 (short *);
- Boolean WritePrefs (long *, short *, prefsInfo *);
- Boolean WritePrefs6 (short *, prefsInfo *);
- OSErr ReadPrefs (long *, short *, prefsInfo *);
- OSErr ReadPrefs6 (short *, prefsInfo *);
- Boolean DeletePrefs (long *, short *);
- Boolean DeletePrefs6 (short *);
-
-
- //============================================================== Functions
- //-------------------------------------------------------------- CanUseFindFolder
-
- Boolean CanUseFindFolder (void)
- {
- OSErr theErr;
- long theFeature;
-
- if (!DoWeHaveGestalt())
- return(FALSE);
-
- theErr = Gestalt(gestaltFindFolderAttr, &theFeature);
- if (theErr != noErr)
- return(FALSE);
-
- if (!BitTst(&theFeature, 31 - gestaltFindFolderPresent))
- return(FALSE);
- else
- return(TRUE);
- }
-
- //-------------------------------------------------------------- GetPrefsFPath
-
- Boolean GetPrefsFPath (long *prefDirID, short *systemVolRef)
- {
- OSErr theErr;
-
- theErr = FindFolder(kOnSystemDisk, kPreferencesFolderType, kCreateFolder,
- systemVolRef, prefDirID);
- if (theErr != noErr)
- return(FALSE);
-
- return(TRUE);
- }
-
- //-------------------------------------------------------------- CreatePrefsFolder
-
- Boolean CreatePrefsFolder (short *systemVolRef)
- {
- HFileParam fileParamBlock;
- Str255 folderName;
- OSErr theErr;
-
- GetIndString(folderName, kPrefsStringsID, kPrefsFNameIndex);
-
- fileParamBlock.ioVRefNum = *systemVolRef;
- fileParamBlock.ioDirID = 0;
- fileParamBlock.ioNamePtr = folderName;
- fileParamBlock.ioCompletion = 0L;
-
- theErr = PBDirCreate((HParmBlkPtr)&fileParamBlock, FALSE);
- if (theErr != noErr)
- {
- RedAlert("\pPrefs Creation Error");
- return(FALSE);
- }
- return(TRUE);
- }
-
- //-------------------------------------------------------------- GetPrefsFPath6
-
- Boolean GetPrefsFPath6 (short *systemVolRef)
- {
- Str255 folderName;
- SysEnvRec thisWorld;
- CInfoPBRec catalogInfoPB;
- DirInfo *directoryInfo = (DirInfo *) &catalogInfoPB;
- HFileInfo *fileInfo = (HFileInfo *) &catalogInfoPB;
- WDPBRec workingDirPB;
- long prefDirID;
- OSErr theErr;
-
- GetIndString(folderName, kPrefsStringsID, kPrefsFNameIndex);
-
- theErr = SysEnvirons(2, &thisWorld);
- if (theErr != noErr)
- return(FALSE);
-
- *systemVolRef = thisWorld.sysVRefNum;
- fileInfo->ioVRefNum = *systemVolRef;
- fileInfo->ioDirID = 0;
- fileInfo->ioFDirIndex = 0;
- fileInfo->ioNamePtr = folderName;
- fileInfo->ioCompletion = 0L;
- theErr = PBGetCatInfo(&catalogInfoPB, FALSE);
- if (theErr != noErr)
- {
- if (theErr != fnfErr)
- {
- RedAlert("\pPrefs Filepath Error");
- }
- if (!CreatePrefsFolder(systemVolRef))
- return(FALSE);
-
- directoryInfo->ioVRefNum = *systemVolRef;
- directoryInfo->ioFDirIndex = 0;
- directoryInfo->ioNamePtr = folderName;
- theErr = PBGetCatInfo(&catalogInfoPB, FALSE);
- if (theErr != noErr)
- {
- RedAlert("\pPrefs GetCatInfo() Error");
- return(FALSE);
- }
- }
- prefDirID = directoryInfo->ioDrDirID;
-
- workingDirPB.ioNamePtr = nil;
- workingDirPB.ioVRefNum = *systemVolRef;
- workingDirPB.ioWDIndex = 0;
- workingDirPB.ioWDProcID = 0;
- workingDirPB.ioWDVRefNum = 0;
- workingDirPB.ioCompletion = 0L;
- theErr = PBGetWDInfo(&workingDirPB, FALSE);
- if (theErr != noErr)
- {
- RedAlert("\pPrefs PBGetWDInfo() Error");
- }
-
- *systemVolRef = workingDirPB.ioWDVRefNum;
-
- workingDirPB.ioNamePtr = nil;
- workingDirPB.ioWDDirID = prefDirID;
- workingDirPB.ioVRefNum = *systemVolRef;
- workingDirPB.ioWDProcID = 0;
- workingDirPB.ioCompletion = 0L;
- theErr = PBOpenWD(&workingDirPB, FALSE);
- if (theErr != noErr)
- {
- RedAlert("\pPrefs PBOpenWD() Error");
- }
-
- *systemVolRef = workingDirPB.ioVRefNum;
-
- return(TRUE);
- }
-
- //-------------------------------------------------------------- WritePrefs
-
- Boolean WritePrefs (long *prefDirID, short *systemVolRef, prefsInfo *thePrefs)
- {
- OSErr theErr;
- short fileRefNum;
- long byteCount;
- FSSpec theSpecs;
- Str255 fileName = kPrefFileName;
-
- theErr = FSMakeFSSpec(*systemVolRef, *prefDirID, fileName, &theSpecs);
- if (theErr != noErr)
- {
- if (theErr != fnfErr)
- {
- RedAlert("\pPrefs FSMakeFSSpec() Error");
- }
- theErr = FSpCreate(&theSpecs, kPrefCreatorType, kPrefFileType, smSystemScript);
- if (theErr != noErr)
- {
- RedAlert("\pPrefs FSpCreate() Error");
- }
- }
- theErr = FSpOpenDF(&theSpecs, fsRdWrPerm, &fileRefNum);
- if (theErr != noErr)
- {
- RedAlert("\pPrefs FSpOpenDF() Error");
- }
-
- byteCount = sizeof(*thePrefs);
-
- theErr = FSWrite(fileRefNum, &byteCount, thePrefs);
- if (theErr != noErr)
- {
- RedAlert("\pPrefs FSWrite() Error");
- }
-
- theErr = FSClose(fileRefNum);
- if (theErr != noErr)
- {
- RedAlert("\pPrefs FSClose() Error");
- }
-
- return(TRUE);
- }
-
- //-------------------------------------------------------------- WritePrefs6
-
- Boolean WritePrefs6 (short *systemVolRef, prefsInfo *thePrefs)
- {
- OSErr theErr;
- short fileRefNum;
- long byteCount;
- Str255 fileName = kPrefFileName;
-
- theErr = FSOpen(fileName, *systemVolRef, &fileRefNum);
- if (theErr != noErr)
- {
- if (theErr != fnfErr)
- {
- RedAlert("\pPrefs FSOpen() Error");
- }
- theErr = Create(fileName, *systemVolRef, kPrefCreatorType, kPrefFileType);
- if (theErr != noErr)
- {
- RedAlert("\pPrefs Create() Error");
- }
- theErr = FSOpen(fileName, *systemVolRef, &fileRefNum);
- if (theErr != noErr)
- {
- RedAlert("\pPrefs FSOpen() Error");
- }
- }
-
- byteCount = sizeof(*thePrefs);
-
- theErr = FSWrite(fileRefNum, &byteCount, thePrefs);
- if (theErr != noErr)
- {
- RedAlert("\pPrefs FSWrite() Error");
- }
-
- theErr = FSClose(fileRefNum);
- if (theErr != noErr)
- {
- RedAlert("\pPrefs FSClose() Error");
- }
-
- return(TRUE);
- }
-
- //-------------------------------------------------------------- SavePrefs
-
- Boolean SavePrefs (prefsInfo *thePrefs, short versionNow)
- {
- long prefDirID;
- short systemVolRef;
- Boolean canUseFSSpecs;
-
- thePrefs->prefVersion = versionNow;
-
- canUseFSSpecs = CanUseFindFolder();
- if (canUseFSSpecs)
- {
- if (!GetPrefsFPath(&prefDirID, &systemVolRef))
- return(FALSE);
- }
- else
- {
- if (!GetPrefsFPath6(&systemVolRef))
- return(FALSE);
- }
-
- if (canUseFSSpecs)
- {
- if (!WritePrefs(&prefDirID, &systemVolRef, thePrefs))
- return(FALSE);
- }
- else
- {
- if (!WritePrefs6(&systemVolRef, thePrefs))
- return(FALSE);
- }
-
- return(TRUE);
- }
-
- //-------------------------------------------------------------- ReadPrefs
-
- OSErr ReadPrefs (long *prefDirID, short *systemVolRef, prefsInfo *thePrefs)
- {
- OSErr theErr;
- short fileRefNum;
- long byteCount;
- FSSpec theSpecs;
- Str255 fileName = kPrefFileName;
-
- theErr = FSMakeFSSpec(*systemVolRef, *prefDirID, fileName, &theSpecs);
- if (theErr != noErr)
- {
- if (theErr == fnfErr)
- return(theErr);
- else
- {
- RedAlert("\pPrefs FSMakeFSSpec() Error");
- }
- }
-
- theErr = FSpOpenDF(&theSpecs, fsRdWrPerm, &fileRefNum);
- if (theErr != noErr)
- {
- RedAlert("\pPrefs FSpOpenDF() Error");
- }
-
- byteCount = sizeof(*thePrefs);
-
- theErr = FSRead(fileRefNum, &byteCount, thePrefs);
- if (theErr != noErr)
- {
- if (theErr == eofErr)
- theErr = FSClose(fileRefNum);
- else
- {
- RedAlert("\pPrefs FSRead() Error");
- }
- return(theErr);
- }
-
- theErr = FSClose(fileRefNum);
- if (theErr != noErr)
- {
- RedAlert("\pPrefs FSClose() Error");
- }
-
- return(theErr);
- }
-
- //-------------------------------------------------------------- ReadPrefs6
-
- OSErr ReadPrefs6 (short *systemVolRef, prefsInfo *thePrefs)
- {
- OSErr theErr;
- short fileRefNum;
- long byteCount;
- Str255 fileName = kPrefFileName;
-
- theErr = FSOpen(fileName, *systemVolRef, &fileRefNum);
- if (theErr != noErr)
- {
- if (theErr == fnfErr)
- return(theErr);
- else
- {
- RedAlert("\pPrefs FSOpen() Error");
- }
- }
-
- byteCount = sizeof(*thePrefs);
-
- theErr = FSRead(fileRefNum, &byteCount, thePrefs);
- if (theErr != noErr)
- {
- if (theErr == eofErr)
- theErr = FSClose(fileRefNum);
- else
- {
- RedAlert("\pPrefs FSRead() Error");
- }
- return(theErr);
- }
-
- theErr = FSClose(fileRefNum);
- if (theErr != noErr)
- {
- RedAlert("\pPrefs FSClose() Error");
- }
-
- return(theErr);
- }
-
- //-------------------------------------------------------------- DeletePrefs
-
- Boolean DeletePrefs (long *dirID, short *volRef)
- {
- FSSpec theSpecs;
- Str255 fileName = kPrefFileName;
- OSErr theErr;
-
- theErr = FSMakeFSSpec(*volRef, *dirID, fileName, &theSpecs);
- if (theErr != noErr)
- return(FALSE);
- else
- theErr = FSpDelete(&theSpecs);
-
- if (theErr != noErr)
- return(FALSE);
-
- return(TRUE);
- }
-
- //-------------------------------------------------------------- DeletePrefs6
-
- Boolean DeletePrefs6 (short *volRef)
- {
- Str255 fileName = kPrefFileName;
- OSErr theErr;
-
- theErr = FSDelete(fileName, *volRef);
-
- if (theErr != noErr)
- return(FALSE);
-
- return(TRUE);
- }
-
- //-------------------------------------------------------------- LoadPrefs
-
- Boolean LoadPrefs (prefsInfo *thePrefs, short versionNeed)
- {
- long prefDirID;
- OSErr theErr;
- short systemVolRef;
- Boolean canUseFSSpecs, noProblems;
-
- canUseFSSpecs = CanUseFindFolder();
- if (canUseFSSpecs)
- {
- noProblems = GetPrefsFPath(&prefDirID, &systemVolRef);
- if (!noProblems)
- return(FALSE);
- }
- else
- {
- noProblems = GetPrefsFPath6(&systemVolRef);
- if (!noProblems)
- return(FALSE);
- }
-
- if (canUseFSSpecs)
- {
- theErr = ReadPrefs(&prefDirID, &systemVolRef, thePrefs);
- if (theErr == eofErr)
- {
- noProblems = DeletePrefs(&prefDirID, &systemVolRef);
- return(FALSE);
- }
- else if (theErr != noErr)
- return(FALSE);
- }
- else
- {
- theErr = ReadPrefs6(&systemVolRef, thePrefs);
- if (theErr == eofErr)
- {
- noProblems = DeletePrefs6(&systemVolRef);
- return(FALSE);
- }
- else if (theErr != noErr)
- return(FALSE);
- }
-
- if (thePrefs->prefVersion != versionNeed)
- {
- if (canUseFSSpecs)
- {
- noProblems = DeletePrefs(&prefDirID, &systemVolRef);
- return(FALSE);
- }
- else
- {
- noProblems = DeletePrefs6(&systemVolRef);
- return(FALSE);
- }
- }
-
- return(TRUE);
- }
-
-